package com.xqls.biz.impl;

import com.xqls.bean.CostBean;
import com.xqls.biz.CostBiz;
import com.xqls.biz.RegisterBiz;
import com.xqls.service.CostService;
import com.xqls.util.CommonPage;
import com.xqls.util.CommonResult;
import com.xqls.util.VelocityUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CostBizImpl implements CostBiz{

	@Autowired private CostService service;
	
	@Autowired TransactionTemplate transactionTemplate;
	
	private static Logger logger = Logger.getLogger(RegisterBiz.class);
	
	@Override
	public CommonResult getCostByPage(CommonPage commonPage, HttpServletRequest request, HttpServletResponse response) {
		CommonResult result = new CommonResult();
		try {
			List<Map<String,Object>> costData=service.queryByMap(commonPage.pageToMap());
			int curr=commonPage.getCurr();
			for (int i=0;i<costData.size();i++){
				costData.get(i).put("viewId",i+1+((curr-1)*15));
			}
			commonPage.setPageData(costData);
			Map<String,Object> showMap=new HashMap<String,Object>();
			showMap.put("costList",commonPage.getPageData());
			commonPage.setHtml(VelocityUtil.getHtmlByMap("cost/costListData.vm",showMap,request,response,request.getSession().getServletContext()));
			result.setIsSuccess(true);
		} catch (Exception e) {
			logger.error("分页条件查询取样异常",e);
		}
		return result;
	}



	@Override
	public CommonResult add(CostBean costBean) {
		CommonResult result = new CommonResult();
		boolean success = false;
		try {
			success = service.add(costBean);
		} catch (Exception e) {
			logger.error("增加费用异常",e);
		}
		result.setIsSuccess(success);
		return result;
	}
	
	@Override
	public CommonResult getAll(CommonPage commonPage, HttpServletRequest request, HttpServletResponse response) {
		CommonResult result = new CommonResult();
		try {
			commonPage.setPageData(service.getmap(commonPage.pageToMap()));
			Map<String,Object> showMap=new HashMap<String,Object>();
			showMap.put("costList",commonPage.getPageData());
			commonPage.setHtml(VelocityUtil.getHtmlByMap("flowmeter/costList.vm",showMap,request,response,request.getSession().getServletContext()));
			result.setIsSuccess(true);
		} catch (Exception e) {
			logger.error("分页条件查询费用异常",e);
		}
		return result;
	}
	
	@Override
	public CostBean selectByKey(Integer Key) {
		CostBean bean = null;
		try {
			bean = service.selectByKey(Key);
		} catch (Exception e){
			logger.error("根据主键查询费用异常",e);
		}
		return bean;
	}
	@Override
	public CommonResult update(CostBean costBean) {
		CommonResult result = new CommonResult();
		boolean success = false;
		try {
			success = service.update(costBean);
		} catch (Exception e) {
			logger.error("修改费用异常",e);
		}
		result.setIsSuccess(success);
		return result;
	}

	@Override
	public List<Map<String, Object>> queryByMap(Map<String, Object> map) {
		List<Map<String, Object>> beanMap = null;
		try {
			beanMap = service.queryByMap(map);
		} catch (Exception e) {
			logger.error("根据map查询取样异常",e);
		}
		return beanMap;
	}
	
	

}
